package com.appkubes.mall.admin.application.system.attachment.service;

import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.admin.application.system.attachment.dao.AttachmentDao;
import com.appkubes.mall.core.po.MallAttachment;
import com.appkubes.mall.core.po.MallAttachmentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class AttachmentService {

    @Autowired
    AttachmentDao attachmentDao;

    public PageInfo querySelective(String groupKey, String filename, Integer page, Integer size, String sort, String order) {
        MallAttachmentExample example = new MallAttachmentExample();
        MallAttachmentExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(groupKey)) {
            criteria.andGroupKeyEqualTo(groupKey);
        }
        if (!StringUtils.isEmpty(filename)) {
            criteria.andFilenameLike("%" + filename + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, size);
        List<MallAttachment> list = attachmentDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public MallAttachment findById(String sassattachmentKey) {
        return attachmentDao.selectByPrimaryKey(sassattachmentKey);
    }

    public int add(MallAttachment model)
    {
        model.setAttachmentKey(UUIDUtils.getUUID());
        model.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        model.setAppKey(null);
        return attachmentDao.insertSelective(model);
    }

    public int update(MallAttachment model) {
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        return attachmentDao.updateByPrimaryKeySelective(model);
    }



    public int deleteByIds(String[] ids) {
        return attachmentDao.delete(ids);
    }



}
